﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using AssetsSys.BLL.AutoMapper;
using AssetsSys.DAL;
using AssetsSys.Dto;
using AssetsSys.Dto.Assets;
using AssetsSys.Dto.Fixup;
using AssetsSys.Dto.Status;
using AssetsSys.model;

namespace AssetsSys.BLL
{
    /// <summary>
    /// 维修记录
    /// </summary>
    public class FixupManger
    {
        /// <summary>
        /// 获取个人报修列表
        /// </summary>
        /// <returns></returns>
        public static async Task<PageModel> GetList(FixupQuertDto request, Guid userId)
        {
            using (var fixSvc = new FixupService())
            {
                Expression<Func<Fixup, bool>> predicate = (x => x.IsRemoved == false
                                                                 && (request.OldStatusName == null || x.OldState.StatusName.Contains(request.OldStatusName))
                                                                 && (request.AssetsNum == null || x.Assets.AssetsNum.Contains(request.AssetsNum))
                                                                 && (request.AssetsName == null || x.Assets.AssetsName.Contains(request.AssetsName))
                                                                 && (request.FixupState == null || x.FixupState.Contains(request.FixupState))
                                                                 && x.RepairId == userId);
                var totalCount = (await fixSvc.GetAll(predicate, true).ToListAsync()).Count;
                var department = await fixSvc.GetAll(predicate, false, request.PageIndex, request.PageSize).Select(x => new FixupResponseModel()
                {
                    Id = x.Id,
                    AssetsName = x.Assets.AssetsName,
                    AssetsNum = x.Assets.AssetsNum,
                    OldState = x.OldState.StatusName,
                    NewState = x.NewStateId == null ? null : x.NewState.StatusName,
                    FixUsers = x.FixId == null ? null : x.FixUsers.UserName,
                    FixDepartment = x.FixId == null ? null : x.FixUsers.Department.DepartmentName,
                    FixupDescribe = x.FixupDescribe,
                    FixupState = x.FixupState,
                    RepairUsers = x.RepairUsers.UserName,
                    RepairDepartment = x.RepairUsers.Department.DepartmentName,
                    CreateTime = x.CreateTime
                }).ToListAsync();
                return new PageModel()
                {
                    Count = totalCount,
                    Data = department
                };
            }
        }

        /// <summary>
        /// 获取待列表
        /// </summary>
        /// <returns></returns>
        public static async Task<PageModel> GetFixupList(FixupQuertDto request, Guid userId)
        {
            using (var fixSvc = new FixupService())
            {
                Expression<Func<Fixup, bool>> predicate = (x => x.IsRemoved == false

                                                                 && (request.FixupState == null||x.FixupState==request.FixupState)
                                                                 && (request.OldStatusName == null || x.OldState.StatusName.Contains(request.OldStatusName))
                                                                 && (request.AssetsNum == null || x.Assets.AssetsNum.Contains(request.AssetsNum))
                                                                 && (request.AssetsName == null || x.Assets.AssetsName.Contains(request.AssetsName)));
                var totalCount = (await fixSvc.GetAll(predicate, true).ToListAsync()).Count;
                var department = await fixSvc.GetAll(predicate, false, request.PageIndex, request.PageSize).Select(x => new FixupResponseModel()
                {
                    Id = x.Id,
                    AssetsName = x.Assets.AssetsName,
                    AssetsNum = x.Assets.AssetsNum,
                    OldState = x.OldState.StatusName,
                    NewState = x.NewStateId == null ? null : x.NewState.StatusName,
                    FixUsers = x.FixId == null ? null : x.FixUsers.UserName,
                    FixDepartment = x.FixUsers.Department.DepartmentName,
                    FixupDescribe = x.FixupDescribe,
                    FixupState = x.FixupState,
                    RepairUsers = x.RepairUsers.UserName,
                    RepairDepartment = x.RepairUsers.Department.DepartmentName,
                    CreateTime = x.CreateTime
                }).ToListAsync();
                return new PageModel()
                {
                    Count = totalCount,
                    Data = department
                };
            }
        }



        /// <summary>
        /// 提交维修申请
        /// </summary>
        /// <param name="assetsId"></param>
        /// <param name="repairId"></param>
        /// <param name="fixupDescribe"></param>
        /// <returns></returns>
        public static async Task<bool> AddFixup(Guid assetsId, Guid repairId, string fixupDescribe)
        {
            Assets asstes;
            using (var assSvc = new AssetsService())
            {
                asstes = await assSvc.GetOne(assetsId);
            }
            asstes.StatusId = Guid.Parse("3093BBE0-3F9B-4601-8EF3-153159C0DF64");
            await AssetsManger.Update(asstes.MapTo<AssetsModel>());
            using (var fixSvc = new FixupService())
            {
                await fixSvc.CreateAsync(new Fixup()
                {
                    AssetsId = assetsId,
                    OldStateId = asstes.StatusId,
                    NewStateId = null,
                    FixupDescribe = fixupDescribe,
                    FixupState = "0",
                    RepairId = repairId,
                    FixId = null
                });
                return true;
            }
        }

        /// <summary>
        /// 结束维修
        /// </summary>
        /// <param name="id"></param>
        /// <param name="fixId"></param>
        /// <param name="newState"></param>
        /// <param name="fixupDescribe"></param>
        /// <returns></returns>
        public static async Task<bool> Update(Guid id, Guid fixId, Guid newState, string fixupDescribe)
        {
            using (var fixSvc = new FixupService())
            {
                var oldModel = await fixSvc.GetOne(id);
                if (oldModel == null)
                {
                    return false;
                }
                oldModel.FixId = fixId;
                oldModel.NewStateId = newState;
                oldModel.FixupState = "1";
                oldModel.FixupDescribe = oldModel.FixupDescribe + "  维修结果：" + fixupDescribe;
                await fixSvc.EditAsvnc(oldModel);
                var asseteModel = new AssetsModel()
                {
                    Id = oldModel.AssetsId,
                    StatusId = newState,
                };
                await AssetsManger.Update(asseteModel);
            }
            return true;
        }

    }
}
